package eclipse.lib.sockets;
import eclipse.*;
import eclipse.client.*;
import eclipse.lib.scripting.*;

import javax.swing.*;
/**
 * The ClientDataHandle class handles all data that is sent to the Client from the server.
 */
public class ClientDataHandle
{
	private static final char msg_REQUESTMAX 		= 0;
	private static final char msg_REQUESTNEWACCOUNT	= 1;
	private static final char msg_SENDCHARPOS		= 2;
	private static final char msg_REQUESTLOGIN		= 3;
	/** 
	 *  Processes a new net message sent to the Client.
	 * @param netMsg String representing the message recieved.
	 */
	public static void process(String netMsg)
	{		
		if(netMsg.charAt(0) == msg_REQUESTMAX)
			recieveMaxData(netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTNEWACCOUNT)
			recieveNewAccountConf(netMsg);
		else if(netMsg.charAt(0) == msg_SENDCHARPOS)
			recieveCharPosition(netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTLOGIN)
			recieveLoginConf(netMsg);
	}
	/**
	 * Message processed if the MaxData was recieved
	 * @param netMsg String representing the message recieved.
	 */
	public static void recieveMaxData(String netMsg)
	{
		Database.MAX_PLAYERS 	= ByteInterpreter.getIntFromString(netMsg, 1);
		Database.MAX_ITEMS		= ByteInterpreter.getIntFromString(netMsg, 3);
		Database.MAX_MAPS		= ByteInterpreter.getIntFromString(netMsg, 5);
		Database.MAX_NPCS		= ByteInterpreter.getIntFromString(netMsg, 7);
		Database.MAX_SHOPS		= ByteInterpreter.getIntFromString(netMsg, 9);
		Database.MAX_SPELLS		= ByteInterpreter.getIntFromString(netMsg, 11);
		Database.MAX_CLASSES	= ByteInterpreter.getIntFromString(netMsg, 13);
		Database.myID			= ByteInterpreter.getIntFromString(netMsg, 15);
		System.out.println(Database.myID);
		Database.AllDataReceived = true;
		Database.player = new eclipse.lib.player.Player[Database.MAX_PLAYERS];
		for(int i = 0; i < Database.MAX_PLAYERS; i++)			
			Database.player[i] = new eclipse.lib.player.Player();
	}
	/**
	 * Packet recieved after a new Account was created
	 * @param netMsg String representing the message recieved. 
	 */
	public static void recieveNewAccountConf(String netMsg)
	{
		if(netMsg.charAt(1) == 't')
			JOptionPane.showMessageDialog(null, "Account Created!");
		else
			JOptionPane.showMessageDialog(null, "An error occurred while creating your account.");
	}
	/** 
	 * Pakcet sent when requesting a new Account.
	 *
	 */
	public static void requestNewAccount()
	{
		String msg = msg_REQUESTNEWACCOUNT + "";
		msg = msg + 
			Database.USERNAME + 
			eclipse.Constants.SEP_CHAR + 
			Database.PASSWORD + 
		"\n";
		Database.Socket.sendMsg(msg);
	}
	/**
	 * Packet sent when requesting the max data.
	 *
	 */
	public static void requestMaxData()
	{
		String msg = msg_REQUESTMAX + "\n";		
		Database.Socket.sendMsg(msg);				
	}		
	/**
	 * Packet sent when sending the character position.
	 *
	 */
	public static void sendCharPosition()
	{
		String msg = msg_SENDCHARPOS + "";
		int playerID = Database.myID;
		int dir 	= Database.player[playerID].character.cDir;
		int frame	= Database.player[playerID].character.cFrame;
		float xPos 	= Database.player[playerID].character.cPos.x;
		float yPos 	= Database.player[playerID].character.cPos.y;
		float zPos 	= Database.player[playerID].character.cPos.z;
		msg = msg + ByteInterpreter.appendIntegerToString(dir);
		msg = msg + ByteInterpreter.appendIntegerToString(frame);
		msg = msg + ByteInterpreter.appendFloatToString(xPos);
		msg = msg + ByteInterpreter.appendFloatToString(yPos);
		msg = msg + ByteInterpreter.appendFloatToString(zPos);
		msg = msg + "\n";
		Database.Socket.sendMsg(msg);
	}
	/**
	 * Message processed when a character moves.
	 * @param netMsg String representing the message recieved.
	 */
	public static void recieveCharPosition(String netMsg)
	{
		int index	= ByteInterpreter.getIntFromString(netMsg, 1);
		int dir 	= ByteInterpreter.getIntFromString(netMsg, 3);
		int frame	= ByteInterpreter.getIntFromString(netMsg, 5);
		float xPos 	= ByteInterpreter.getFloatFromString(netMsg, 7);
		float yPos 	= ByteInterpreter.getFloatFromString(netMsg, 9);
		float zPos 	= ByteInterpreter.getFloatFromString(netMsg, 11);	
		if(index == Database.myID)
			return;		
		Database.player[index].character.cDir 	= dir;
		Database.player[index].character.cFrame = frame;
		Database.player[index].character.cPos.x = xPos;
		Database.player[index].character.cPos.y = yPos;
		Database.player[index].character.cPos.z = zPos;
		//System.out.println(xPos);
	}
	public static void requestLogin()	
	{
		String msg = msg_REQUESTLOGIN + "";
		msg = msg + 
			Database.USERNAME + 
			eclipse.Constants.SEP_CHAR + 
			Database.PASSWORD + 
		"\n";
		Database.Socket.sendMsg(msg);
	}
	public static void recieveLoginConf(String netMsg)
	{
		if(netMsg.charAt(1) == 't')
		{
			Database.Login.setVisible(false);
			Database.Mirage.setAlwaysOnTop(true);
			Database.MENUSTATE = Constants.MENUSTATE_CHARSELECT;
		}
		else
			JOptionPane.showMessageDialog(null, "An error occurred while logging in.");
	}
}